$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'jykcxx/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: "id", width: 45, key: true,hidden:true },
			{ label: '名称', name: 'mc', index: 'MC', width: 80 }, 
			{ label: '介绍', name: 'js', index: 'JS', width: 80 }, 
			{ label: '备注', name: 'bz', index: 'BZ', width: 80 }, 
			{ label: '类别', name: 'lb', index: 'LB', width: 80,formatter: function(value, options, row){
                var str="";
                for(var i in vm.lb){
                    if(vm.lb[i].value===value){
                        str=vm.lb[i].text;
                    }
                }
                return  str;
            } },
			{ label: '考核方式', name: 'khfs', index: 'KHFS', width: 80,formatter: function(value, options, row){
                var str="";
                for(var i in vm.khfs){
                    if(vm.khfs[i].value===value){
                        str=vm.khfs[i].text;
                    }
                }
                return  str;
            } },
			{ label: '状态', name: 'status', index: 'STATUS', width: 80,formatter: function(value, options, row){
                var str="";
                for(var i in vm.status){
                    if(vm.status[i].value===value){
                        str=vm.status[i].text;
                    }
                }
                return  str;
            } }
        ],
		viewrecords: true,
        height: 365,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{

        },
		showList: true,
		title: null,
        lb:[{text:'请选择',value:'',selected:'selected'},{text:'专业课',value:'0'},{text:'专业基础课',value:'1'},{text:'公共课',value:'2'},
            {text:'选修课',value:'4'}],
        khfs:[{text:'请选择',value:''},{text:'考试课',value:'0'},{text:'考查课',value:'1'}],
		jyKcxx: {},
        status:[{text:'请选择',value:''},{text:'启用',value:'0'},{text:'未启用',value:'1'}],
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.jyKcxx = {};
		},
		update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.jyKcxx.id == null ? "jykcxx/save" : "jykcxx/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.jyKcxx),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "jykcxx/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "jykcxx/info/"+id, function(r){
                vm.jyKcxx = r.jyKcxx;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
                postData:{'name':vm.q.name}
            }).trigger("reloadGrid");
		}
	}
});